package com.onetech.adrewards.free.models;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.onetech.adrewards.free.OneTech_ApplicationContext;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.config.AppConfig.Cache;
import com.onetech.adrewards.free.config.AppConfig.URLRequestApi;
import com.onetech.adrewards.free.http.APIRequestResult;
import com.onetech.adrewards.free.http.ZuZuHttpRequest;
import com.onetech.adrewards.free.listener.ListGiftCardIF;
import com.onetech.adrewards.free.listener.PointHistoryIF;
import com.onetech.adrewards.free.objects.ListGiftCardDTO;
import com.onetech.adrewards.free.objects.NewsDTO;
import com.onetech.adrewards.free.objects.PointHistoryDTO;
import com.onetech.adrewards.free.objects.UserInfoDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.zuzu.db.store.SimpleStoreIF;

public class ListGiftCardModel implements ListGiftCardIF {
	private static ListGiftCardIF _instance;
	private static final Lock createLock = new ReentrantLock();
	private static final int STORE_EXPIRE = 2 * 60;

	public ListGiftCardModel() {
		getStoreAdapter();
	}

	public static ListGiftCardIF getInstance() {
		if (_instance == null) {
			createLock.lock();
			if (_instance == null) {
				_instance = new ListGiftCardModel();
			}
			createLock.unlock();
		}
		return _instance;
	}

	private SimpleStoreIF getStoreAdapter() {
		return AdrewadsUtils.getStoreAdapter(Cache.POINT_HISTORY_NAME,
				OneTech_ApplicationContext.getContext(), Cache.POINT_HISTORY_NUMBER);
	}

	public void setStore(String key, Object item) {
		if (item == null) {
			return;
		}
		Gson gson = new Gson();
		String temp = gson.toJson(item);
		this.getStoreAdapter().put(key, temp, STORE_EXPIRE);
	}

	

	@SuppressWarnings("unchecked")
	private List<ListGiftCardDTO> deserializeInteger(String json) {
		List<ListGiftCardDTO> result = null;
		if (json == null || json.equals(""))
			return result;
		try {
			result = new ArrayList<ListGiftCardDTO>();
			Gson gson = new Gson();
			Type listType = new TypeToken<List<ListGiftCardDTO>>() {
			}.getType();
			result = (List<ListGiftCardDTO>) gson.fromJson(json, listType);
		} catch (Exception e) {
			return null;
		}
		return result;
	}

	@Override
	public List<ListGiftCardDTO> getListGiftCard(String pageNo) {
		String json= getStoreAdapter().get(Cache.LIST_GIFTCARD_KEY+pageNo);
		List<ListGiftCardDTO> result = deserializeInteger(json);
		if(result!=null){
			return result;
		}
		Bundle params=new Bundle();
		//AdrewadsUtils.checkUId();
		APIRequestResult data=ZuZuHttpRequest.getInstance().getDataFromServer(URLRequestApi.GET_LIST_GIFT_CARD, params);		
		result = parserListGiftCardFromAPIRequestResult(data);
		if(result!=null){
			setStore(Cache.LIST_GIFTCARD_KEY+pageNo, result);
		}
		return result;
	}	
	
	public UserInfoDTO validate(String memId){
		try {
			UserInfoDTO mem= new UserInfoDTO() ;
			Bundle params = new Bundle();
			APIRequestResult data = ZuZuHttpRequest.getInstance().getDataFromServer(URLRequestApi.GET_USER_CHECK_EMAIL + memId, params);
			if (data.data != null && !data.data.equals(""))
				{
					JSONObject jsonObj = (JSONObject)data.data;
					JSONObject json = jsonObj.getJSONObject("result");
					mem.setEmail(json.getString("email"));
					mem.setTotal_coin(json.getString("total_coin"));
					
				}
			return mem;
		} catch (Exception e) {
			return null;
		}
	}
	private List<ListGiftCardDTO> parserListGiftCardFromAPIRequestResult(APIRequestResult data){
		try {
			List<ListGiftCardDTO> result=null;			
			if (data.error_code.equals("0") && data.data != null && !data.data.equals("")) {				
				JSONObject jsonObject=(JSONObject)data.data;				
				JSONArray jArr=jsonObject.getJSONArray("result");
				result=new ArrayList<ListGiftCardDTO>();
				for (int i = 0; i < jArr.length(); i++) {
					ListGiftCardDTO temp=parserListGiftCardData(jArr.getJSONObject(i));
					if(temp!=null){
						result.add(temp);
					}
				}												
			}
			return result;
		} catch (Exception e) {
			return null;
		}
	}
	
	private ListGiftCardDTO parserListGiftCardData(JSONObject jb){
		try {
			ListGiftCardDTO result=new ListGiftCardDTO();
			result.setId(jb.getInt("id"));
			result.setName(jb.getString("name"));
			//result.setShort_Desc(jb.getString("short_desc"));
			result.setImage(jb.getString("image"));
			result.setCoin(jb.getInt("coin"));
			int discount = jb.getInt("discount");
			if(discount <= 0)
			{
				result.setDiscount(jb.getInt("coin"));
			}
			else
			{
				result.setDiscount(discount);
			}
			result.setPrice(jb.getInt("price"));
			return result;
		} catch (Exception e) {
			return null;
		}
	}
}
